package top.hundreds.oasys.controller.FrontController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Meeting;
import top.hundreds.oasys.bean.enums.MeetingStatus;
import top.hundreds.oasys.bean.vo.MeetingInfoVo;
import top.hundreds.oasys.bean.vo.MeetingVo;
import top.hundreds.oasys.bean.vo.UserVo;
import top.hundreds.oasys.service.MeetingService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * (Meeting)表控制层
 *
 * @author lcf
 * @since 2021-01-27 14:59:55
 */
@RestController
@RequestMapping("/front/meeting")
public class MeetingController {
    /**
     * 服务对象
     */
    @Resource
    private MeetingService meetingService;

    /**
     * 获取单个会议
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/info/{id}")
    public ApiResult<MeetingInfoVo> get(@PathVariable Long id) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        return ApiResult.success(this.meetingService.info(this.meetingService.get(id)));
    }

    /**
     * 获取会议分页
     *
     * @param pageParam 分页参数
     * @return 会议分页
     */
    @PostMapping("/list")
    public ApiResult<List<MeetingVo>> list(@RequestBody PageParam<Meeting> pageParam) {
        Page<Meeting> page = meetingService.getPage(pageParam);
        List<Meeting> records = page.getRecords();
        ArrayList<MeetingVo> list = new ArrayList<>(records.size());
        for (Meeting record : records) {
            list.add(meetingService.buildVo(record));
        }
        return ApiResult.success(list);
    }

    @PostMapping("myMeeting")
    public ApiResult<List<MeetingVo>> myMeeting(@RequestBody PageParam<Meeting> pageParam, @RequestAttribute("id") Long id) {
        System.out.println(id);
        if (pageParam.getAndQuery() != null) {
            Meeting andQuery = pageParam.getAndQuery();
            andQuery.setUserId(id);
            pageParam.setAndQuery(andQuery);
        }
        Page<Meeting> page = this.meetingService.getPage(pageParam);
        List<Meeting> records = page.getRecords();
        List<MeetingVo> list = new ArrayList<>(records.size());
        for (Meeting record : records) {
            list.add(meetingService.buildVo(record));
        }
        return ApiResult.success(list);
    }

    @PostMapping("partMeeting")
    public ApiResult<List<MeetingVo>> partMeeting(@RequestBody PageParam<Meeting> pageParam, @RequestAttribute("id") Long id) {
        List<MeetingVo> partMeeting = this.meetingService.getPartMeeting(id, pageParam);
        return ApiResult.success(partMeeting);
    }

    /**
     * 新增或更新
     *
     * @param meetingInfoVo 会议实体类
     * @return success
     */
    @Transactional
    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody MeetingInfoVo meetingInfoVo) {
        Meeting meeting = new Meeting();
        new Converter<MeetingInfoVo, Meeting>().buildVo(meetingInfoVo, meeting);
        List<UserVo> userList = meetingInfoVo.getParticipantList();
        if (meetingInfoVo.getId() == null) {
            HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            Long userId = Long.parseLong(request.getAttribute("id").toString());
            meeting.setUserId(userId);
            // 新增操作
            this.meetingService.add(meeting, userList);
        } else {
            // 更新操作
            this.meetingService.edit(meeting, userList);
        }
        return ApiResult.success();
    }

    /**
     * 删除会议
     *
     * @param id 会议id
     * @return success
     */
    @DeleteMapping("/remove/{id}")
    public ApiResult<String> remove(@PathVariable Long id) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        // 删除操作
        this.meetingService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/start/{id}")
    public ApiResult<String> startMeeting(@PathVariable Long id, @RequestAttribute("id") Long userId) {
        Meeting meeting = this.meetingService.get(id);
        if (!meeting.getUserId().equals(userId)) {
            return ApiResult.fail("开启会议须由发起人开启");
        }
        if (MeetingStatus.formValue(meeting.getStatus()) == MeetingStatus.NO_START) {
            meeting.setStatus(MeetingStatus.STARTING.getValue());
            meetingService.update(meeting);
            return ApiResult.success();
        } else if (MeetingStatus.formValue(meeting.getStatus()) == MeetingStatus.STARTING) {
            return ApiResult.success("会议进行中");
        } else {
            return ApiResult.success("会议已结束");
        }
    }

    @GetMapping("/end/{id}")
    public ApiResult<String> endMeeting(@PathVariable Long id, @RequestAttribute("id") Long userId) {
        Meeting meeting = this.meetingService.get(id);
        if (!meeting.getUserId().equals(userId)) {
            return ApiResult.fail("结束会议须由发起人结束");
        }
        if (MeetingStatus.formValue(meeting.getStatus()) == MeetingStatus.STARTING) {
            meeting.setStatus(MeetingStatus.FINISHED.getValue());
            meetingService.update(meeting);
            return ApiResult.success();
        } else if (MeetingStatus.formValue(meeting.getStatus()) == MeetingStatus.NO_START) {
            return ApiResult.success("会议未开始");
        } else {
            return ApiResult.success("会议已结束");
        }
    }
}
